package com.lanswon.qzsmk.util.pdfutil;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.Attribute;
import javax.print.attribute.AttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.Copies;

import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Logger;

import com.lanswon.qzsmk.exception.SystemException;

/**
 * PDF打印
 * @author Goldenbee
 *
 */
public class PrintPDFImage {
	
	private final static Logger logger = (Logger) LoggerFactory.getLogger(PrintPDFImage.class);
	
	public void printPDFImage(String fileName){
		logger.info("printPDFFile########start()");
		
		FileInputStream psStream = null; 

		try {
			psStream = new FileInputStream(fileName);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			 String msg1="系统找不到文件异常";
				logger.error(msg1);
				throw new SystemException(SystemException.FileNotFoundException, msg1);

		}
		if(psStream == null){
			return ;
		}
		 //设置打印数据的格式，此处为图片gif格式  
        DocFlavor psInFormat = DocFlavor.INPUT_STREAM.PNG;
      //创建打印数据  
//      DocAttributeSet docAttr = new HashDocAttributeSet();//设置文档属性  
//      Doc myDoc = new SimpleDoc(psStream, psInFormat, docAttr); 
        Doc myDoc = new SimpleDoc(psStream, psInFormat, null);
        
        //设置打印属性  
        PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();  
        aset.add(new Copies(1));//打印份数，1份  
        
      //查找所有打印服务  
        PrintService[] services = PrintServiceLookup.lookupPrintServices(psInFormat, aset); 
        if(services.length==0){
        	logger.error("没有打印服务");
        }
     // this step is necessary because I have several printers configured  
        //将所有查找出来的打印机与自己想要的打印机进行匹配，找出自己想要的打印机  
        PrintService myPrinter = null;  
        for (int i = 0; i < services.length; i++) {  
            logger.debug("service found: " + services[i]);  
            String svcName = services[i].toString();  
            if (svcName.contains("MP280")) {  
                myPrinter = services[i];  
                logger.debug("my printer found: " + svcName);  
                logger.debug("my printer found: " + myPrinter);  
                break;  
            }  
        }  
      //可以输出打印机的各项属性  
        if(myPrinter==null){
        	logger.error("没有找到匹配的打印机");
        }
        AttributeSet att = myPrinter.getAttributes();  
  
        for (Attribute a : att.toArray()) {  
  
            String attributeName;  
            String attributeValue;  
  
            attributeName = a.getName();  
            attributeValue = att.get(a.getClass()).toString();  
  
            logger.debug(attributeName + " : " + attributeValue);  
        } 
        
        if (myPrinter != null) {  
        	logger.debug("myPrinter:"+myDoc.toString());
            DocPrintJob job = myPrinter.createPrintJob();//创建文档打印作业  
            try {  
            	
                job.print(myDoc, aset);//打印文档  
  
            } catch (PrintException pe) {  
            	pe.printStackTrace();
            	 String msg1="系统打印文件异常";
 				logger.error(msg1);
 				throw new SystemException(SystemException.PrintException, msg1);

            }  
        }
	}
}
